package edu.gatech.arktos;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextArea;

/**
 * 
 * @author Daranhdara Hun
 */
public class GradesToolPanel {

	/*********************************************************************************************/	
	/**Start of Main**/
	/*********************************************************************************************/	
	/**
	 * Our Main Method
	 * @param args
	 * @author Daranhdara Hun
	 */
	public Student curStudent;
	private String[] comp;
	final JComboBox c = new JComboBox();
	public static void main(String[] args) {



		/* All Variables */
		GradesToolPanel panel = new GradesToolPanel();
		Session session = new Session();
		GradesDB db = null;
		HashSet<Student> students;
		JFrame frame = new JFrame("Grading Tool"); // Our parent window
		JTextArea text = new JTextArea(); // To hold student detail information
		JButton button = new JButton();

		

		/* Access the spreadsheet and pull all student information */
		session.login("cs3300team01@gmail.com ", "bE48CTZb1L");
		db = session.getDBByName(Constants.GRADES_DB);
		students = db.getStudents();

		/* Testing area */
		db.getProjectByName("P1");


		/* Testing area */

		/* Create Dimension Objects */
		Dimension textDim = new Dimension(),frameDim = new Dimension() ;
		textDim.setSize(200,200); //For TextField
		frameDim.setSize(400,400);

		/* Setup Textfield */
		text.setPreferredSize(textDim);
		text.setSelectionColor(Color.blue);

		/* Setup our GUI interface */
		frame.setSize(frameDim);
		frame.setLocation(0,0); //Start window at top/left corner
		Container content = frame.getContentPane();
		content.setBackground(Color.GRAY);
		content.setLayout(new FlowLayout());
		content.add(panel.makePullDownMenu(students, text, db)); // add pulldown menu of student names
		content.add(text); // add text field holding student information
		frame.setVisible(true);


	}
	/*********************************************************************************************/	
	/**End of Main**/
	/*********************************************************************************************/	


	/**
	 * Give a list of Strings, will return a populated JComboBox Object with listener/handler.
	 * @param list - List of items to fill JComboBox
	 * @return JComboBox
	 * @author Daranhdara Hun
	 */
	public JComboBox makePullDownMenu(HashSet<Student> students, final JTextArea jt, final GradesDB db){
		int j = 0;
		comp = new String[students.size()];
		// Add names to pulldown menu
		for (Student student : students){
			c.addItem(student.getName());
			comp[j]= student.getName();
			j++;
		}

		/* Setup Listener and Handler to populate textfield with 
		 * Student information from pulldown menu.
		 */
		c.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// Get name selected from pulldown list
				curStudent = db.getStudentByName(((JComboBox) e.getSource()).getSelectedItem().toString());
				// Add to text field
				jt.setText(updateLabels(curStudent)); 
			}
		});

		return c;
	}
	public String[] returnCombo()
	{
		return this.comp;
	}

	public String updateLabels(Student student) {
		// TODO Auto-generated method stub
		curStudent = student;

		String text="Name: ";
		text += curStudent.getName() + "\n";
		text += "GTID: ";
		text += curStudent.getGtid() + "\n";
		text += "Attendance: ";
		text += curStudent.getAttendance() +"\n";
		return text;

	}


	public void save(Student curStudent) {
		FileWriter outFile;
		PrintWriter out;
		try {
			outFile = new FileWriter(curStudent.getName()+".txt");
			out = new PrintWriter(outFile); 
			out.println("ID: "+ curStudent.id);
			out.println("Name: "+ curStudent.name);
			out.println("Attendance: "+ curStudent.attend);
			out.println("Email: "+ curStudent.email);
			out.println("Average Grade: "+ curStudent.avgGrade);
			out.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}






}
